package lxt.service.impl;

import java.util.ArrayList;
import java.util.List;

import lxt.dao.DriverDao;
import lxt.entity.DriverInfo;
import lxt.entity.Parameter;
import lxt.service.DriverService;
/**
 * 驾驶员管理service
 * @author Lit
 *
 */
public class DriverServiceImpl implements DriverService{
	
	private DriverDao driverDao; 
	
	public boolean addDriver(DriverInfo driver) throws Exception{
		this.driverDao.save(driver);
		return true;
	}

	public boolean deleteDriver(String driverId)throws Exception {
		String[] delId = driverId.split(",");
		for(int i=0;i<delId.length;i++){
			String id = delId[i];
			this.driverDao.delete(DriverInfo.class, id);
		}
		return true;
	}

	public int loadAllDriverCount(String name, String driverLicense) throws Exception{
		return this.driverDao.loadAllDriverCount(name, driverLicense);
	}

	public List<DriverInfo> loadDriver(String name, String driverLicense,
			int offset, int length) throws Exception{
		//对关联查询的List<Object> 进行处理
		List<Object> objects = this.driverDao.loadAllDriver(name, driverLicense, offset, length);
		
		List<DriverInfo> drs = new ArrayList<DriverInfo>() ;
		
		if(objects.size()>0){
			for(int i=0 ;i<objects.size();i++){
				Object[] ob =(Object[]) objects.get(i);
				DriverInfo driver =(DriverInfo) ob[0];
				Parameter parame = (Parameter) ob[1];
				//对显示在页面的值进行处理，放在dao里会直接处理数据库信息
				driver.setLicenseType(parame.getParamValue());
				drs.add(driver);
			}
		}
		return drs;
	}

	public DriverInfo loadDriverById(String driverId) throws Exception{
		return (DriverInfo) this.driverDao.get(DriverInfo.class, driverId);
	}

	public boolean updateDriver(DriverInfo driver)throws Exception {
		this.driverDao.update(driver);
		return true;
	}

	/**
	 * 根据name查找驾驶员
	 */
	public List<DriverInfo> loadDriverByName(String name)throws Exception{
		return this.driverDao.loadDriverByName(name);
	}
	
	/**
	 * 查询驾驶员信息
	 */
	@SuppressWarnings("unchecked")
	public List<DriverInfo> loadAll()throws Exception{
		return (List<DriverInfo>) this.driverDao.loadAll(DriverInfo.class);
	}
	/**
	 * get set
	 * @return
	 */
	public DriverDao getDriverDao() {
		return driverDao;
	}

	public void setDriverDao(DriverDao driverDao) {
		this.driverDao = driverDao;
	}

	
}
